package com.ruibang.glass.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-04-18  19:20
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
public class DataUtils {

    /**
     * @Description: 数据分批
     * @version v1.0
     * @author songJinKang
     * @date 2024-03-28 17:16
     */
    public static <T> List<List<T>> batch(List<T> list, int batchSize) {
        List<List<T>> batches = new ArrayList<>();
        for (int i = 0; i < list.size(); i += batchSize) {
            int end = Math.min(list.size(), i + batchSize);
            List<T> batch = list.subList(i, end);
            batches.add(batch);
        }
        return batches;
    }

    public static Object getByFieldName(Object object, String fieldName) {
        try {
            Field field = object.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(object);
        } catch (Exception e) {
            log.info("根据字段名获取字段值异常");
        }
        return null;
    }

    /**
     * @Description: 判断是否是NANOrInfinity
     * @version v1.0
     * @author songJinKang
     * @date 2023-09-08 16:09
     */
    public static float isNANOrInfinity(float num) {
        return Float.isNaN(num) || Float.POSITIVE_INFINITY == num ? 0.0f : num;
    }
}
